package edu.whcp.comp2301.controller;

import edu.whcp.comp2301.dao.UserDao;
import edu.whcp.comp2301.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/list")
public class UserController {
    @Autowired
    private UserDao userDao;

    @RequestMapping("/user")
    public ModelAndView list() {
        List<User> users = userDao.getAllUsers();
        return new ModelAndView("backend/user-management", "userList", users);
    }

    @RequestMapping(value = "/add/user",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer addUser(User user) {
        userDao.addUser(user);
        return 1;
    }

    @RequestMapping(value = "/del/user/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer delUser(@PathVariable String id) {
        return userDao.delUser(id);
    }

    @RequestMapping(value = "/edit/user/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<User> editUser(@PathVariable String id){
        return userDao.getUser(id);
    }

    @PostMapping(value = "/alter/user",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer alterUser(@RequestBody User user) {
        userDao.alterUser(user);
        return 1;
    }
}
